<template>
  <div class="lin-container">
    <div class="lin-wrap">
      <div class="wrap-label">
        <span class="label">列表</span>
        <div class="operatiion">
          <el-button type="primary" size="small" @click="handleClick('新增')">新增</el-button>
        </div>
      </div>
      <el-table :data="tableData" style="width: 100%" border>
        <el-table-column label="No" type="index" width="60"/>
        <el-table-column prop="indexName" label="标题"/>
        <el-table-column prop="indexImg" label="缩略图">
          <template #default="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="returnImage(scope.row.indexImg)"
              :zoom-rate="1.2"
              fit="cover"
              @click="showBigImage(scope.row.indexImg)"
            />
          </template>
        </el-table-column>
        <el-table-column prop="indexContent" label="内容"/>
        <el-table-column prop="indexSort" label="排序"/>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="handleClick('修改',scope.row)">修改</el-button>
            <el-button link type="danger" plain size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog v-model="dialogFormVisible" :title="dialogTitle">
      <el-form label-width="120px" :model="form" :rules="formRules" ref="ruleFormRef">
        <el-form-item label="标题" prop="indexName">
          <el-input v-model="form.indexName"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="indexContent">
          <el-input type="textarea" v-model="form.indexContent"></el-input>
        </el-form-item>
        <el-form-item label="上传图片" class="uploadimage">
          <upload-imgs
            :queryId="queryId"
            ref="uploadEle"
            :rules="rules"
            :imageFileList="imageFileList"
            @setForm="getForm"
          />
        </el-form-item>
        <el-form-item label="排序" prop="indexSort">
          <el-input v-model="form.indexSort"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submit">提交</el-button>
        </span>
      </template>
    </el-dialog>
    <el-image-viewer
      hide-on-click-modal
      @close="()=>{showViewer = false}"
      v-if="showViewer && viewFileList.length>0"
      :url-list="viewFileList"
      :initial-index="0"
    />
  </div>
</template>

<script>
import UploadImgs from "@/components/UploadImg/index.vue";
import {
  menuGetModuleContent,
  deleteMenu,
  addOrUpdateMenu
} from "@/api/PC-Home.js";
import { ElMessage, ElMessageBox } from "element-plus";
export default {
  name: "ImgsUploadStage",
  components: {
    UploadImgs
  },
  data() {
    return {
      form: {
        indexName: "",
        indexContent: "",
        indexSort: ""
      },
      dialogTitle: "",
      dialogFormVisible: false,
      formLabelWidth: "140px",
      rules: {
        maxSize: 1,
        minWidth: 100,
        minHeight: 100
      },
      formRules: {
        indexName: [{ required: true, message: "请输入标题", trigger: "blur" }],
        indexContent: [
          { required: true, message: "请输入内容", trigger: "blur" }
        ],
        indexSort: [{ required: true, message: "请输入排序", trigger: "blur" }]
      },
      tableData: [],
      showViewer: false,
      viewFileList: [],
      queryId: null,
      file: null,
      imageFileList: []
    };
  },
  mounted() {
    this.getQueryId();
    this.menuGetModuleContent();
  },
  methods: {
    returnImage(url) {
      return process.env.VUE_APP_URL + "/" + url;
    },
    getQueryId() {
      this.queryId = this.$route.query.queryId;
    },
    handleClick(str, scope) {
      this.dialogTitle = str;
      this.dialogFormVisible = true;
      this.$nextTick(() => {
         this.$refs.ruleFormRef.clearValidate()
      })
      this.file = null;
      if (this.dialogTitle === "新增") {
        this.$nextTick(() => {
          this.form.indexName = "";
          this.form.indexSort = "";
          this.form.indexContent = "";
          this.$refs.uploadEle.clearFileList();
        });
      } else {
        this.imageFileList = [];
        this.form.indexName = scope.indexName;
        this.form.indexSort = scope.indexSort;
        this.form.indexContent = scope.indexContent;
        this.form.id = scope.id;
        this.imageFileList.push({
          name: scope.indexName,
          url: process.env.VUE_APP_URL + "/" + scope.indexImg
        });
      }
    },
    showBigImage(url) {
      this.viewFileList = [];
      this.showViewer = true;
      this.viewFileList.push(process.env.VUE_APP_URL + "/" + url);
    },
    getForm(value) {
      console.log(value);
      this.file = value;
    },
    submit() {
      let file = this.file;
      let form = null;
      this.$refs.ruleFormRef.validate(valid => {
        if (valid) {
          form = new FormData();
          form.append("file", file ? file.raw : "");
          form.append("parentId", this.queryId);
          form.append("indexName", this.form.indexName);
          form.append("indexContent", this.form.indexContent);
          form.append("indexSort", this.form.indexSort);
          form.append("id", this.form.id ? this.form.id : "");
          addOrUpdateMenu(form).then(res => {
            if (res.code === 200) {
              this.$message.success("上传成功");
              this.menuGetModuleContent();
              this.dialogFormVisible = false;
            } else {
              this.$message.error(res.message);
            }
          });
        } else {
          return false;
        }
      });
    },
    menuGetModuleContent() {
      let param = {
        id: this.queryId
      };
      menuGetModuleContent(param).then(res => {
        this.tableData = res.data;
      });
    },
    handleDelete(item) {
      ElMessageBox.confirm("是否删除该条信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let param = {
            id: item.id
          };
          deleteMenu(param).then(res => {
            if (res.code === 200) {
              ElMessage({
                type: "success",
                message: "删除成功"
              });
              this.menuGetModuleContent();
            }
          });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消删除"
          });
        });
    }
  }
};
</script>

    
<style scoped lang="scss">
.lin-wrap {
  padding: 40px 0;
  .wrap-label {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    .label {
      font-size: 16px;
    }
  }
}
::v-deep .uploadimage {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  .el-form-item__content {
    display: flex;
    flex-direction: column;
    align-items: flex-start;

    .upload-demo {
      display: flex !important;
      flex-direction: row !important;
      align-items: center !important;

      .el-upload {
        margin-right: 10px;
      }
    }
    .demo-image__preview {
      display: flex;
      flex-direction: row;
    }
  }
}
::v-deep .el-dialog__header {
  display: flex;
  .el-dialog__headerbtn {
    top: 0;
  }
}
</style>
